<template>
	<view class="newOrder">
		<view class="form">
			<view class="form_item1">
				<view class="text">
					<text>*</text>
					供应商:
				</view>
				<view class="input"><input v-model="user" type="text" placeholder="请输入供应商名称" /></view>
			</view>
			<view class="form_item1">
				<view class="text">
					<text></text>
					联系电话:
				</view>
				<view class="input"><input v-model="phone" type="text" placeholder="请输入联系电话" /></view>
			</view>
			<view class="form_item1">
				<view class="text">
					<text></text>
					地址:
				</view>
				<view class="input"><input v-model="address" type="text" placeholder="请输入供应商地址" /></view>
			</view>
			<view class="form_item1">
				<view class="text">备注:</view>
				<view class="input"><input v-model="remark" type="text" placeholder="请输入备注" /></view>
			</view>
			<view  class="form_item1">
				<view class="text">置顶:</view>
				<u-switch @change="setTop" v-model="sort"  activeColor="#FE3430" size="48">
				</u-switch>
			</view>
		</view>
		<u-gap height="88"></u-gap>
		<view class="footer_btn">
			<view @tap="submit" class="button-primary">提交</view>
			<view @tap="reset" class="button-primary">重置</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: null,
				user: '',
				phone: '',
				address: '',
				remark: '',
				sort: '',
				sid:""
			};
		},
		onLoad(options) {
			this.id = options.id || null;
			this.user = options.user || '';
			this.phone = options.phone || '';
			this.address = options.address || '';
			this.remark = options.remark || '';
			this.sort = options.sort==='1';
			this.sid = options.sid || '';
		},
		methods: {
			async setTop(e){
				if(!this.id){
					return
				}
				let data = await this.$api.supplierSetTopApi({data:{
					sid:this.sid,
					sort:e?1:99
				}})
				uni.showToast({
					title:data.message,
					icon:"none"
				})
			},
			reset() {
				this.user = '';
				this.phone = '';
				this.address = '';
				this.remark = '';
			},
			submit() {
				if (!this.user) {
					uni.showToast({
						title: '请正确输入供应商名称',
						icon: 'none'
					});
					return;
				}

				// if (!/^1[3456789]\d{9}$/.test(this.phone)) {
				// 	uni.showToast({
				// 		title: '请正确输入联系电话',
				// 		icon: 'none'
				// 	});
				// 	return;
				// }
				// if (!this.address) {
				// 	uni.showToast({
				// 		title: '请正确输入供应商地址',
				// 		icon: 'none'
				// 	});
				// 	return;
				// }

				let query = {
					sname: this.user,
					supMobile: this.phone,
					supAddress: this.address,
					remark: this.remark,
					sort:this.sort?1:99
				};

				if (this.id) {
					query.id = this.id;
					query.sort = this.sort?1:99
				}


				this.$api.supplierEdit(query).then(_ => {
					uni.showToast({
						title: '提交成功',
						icon: 'none',
						mask: true
					});

                    // const pages = getCurrentPages();
                    // const prePage = pages[pages.length - 2];
                    // if (prePage && prePage.route.indexOf('/purchasing/supplier') !== -1) {
                    //     prePage['$vm'].getList && prePage['$vm'].getList(true);
                    // }
                    uni.navigateBack({
                        delta: 1
                    });
				});

			}
		}
	};
</script>

<style lang="scss" scoped>
	.newOrder {
		min-height: calc(100vh - 44px - env(safe-area-inset-top));
		background: #fff;

		.form {
			padding: 32rpx 68rpx 0;

			.form_item1 {
				margin-bottom: 36rpx;

				.text {
					color: #bdbdbd;
					margin-bottom: 24rpx;

					text {
						color: #fe3430;
					}
				}

				.input {
					display: flex;
					align-items: center;

					.note {
						width: 613rpx;
						height: 181rpx;
						background: #f2f2f2;
						border-radius: 47rpx;
					}

					input {
						height: 94rpx;
						width: 100%;
						background: #f2f2f2;
						border-radius: 47rpx;
						text-indent: 1em;
					}

					button {
						width: 73rpx;
						height: 38rpx;
						background: #fe3430;
						border-radius: 49rpx;
						color: #fff;
					}
				}
			}

			.btn {
				display: flex;

				button {
					height: 58rpx;
					line-height: 58rpx;
					background: #fe3430;
					border-radius: 49rpx;
					color: #fff;
					margin-left: 0;
					margin-right: 10rpx;
				}
			}
		}

		.footer_btn {
			display: flex;
			justify-content: space-around;
			position: absolute;
			bottom: 44px;
			width: 100%;
			left: 0;

			.button-primary {
				width: 280rpx;
			}
		}
	}
</style>
